/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.DocumentHandler;
/*     */ import com.zimbra.soap.ProxyTarget;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ProxiedQueryResults
/*     */   extends ZimbraQueryResultsImpl
/*     */ {
/*     */   protected static final int MIN_BUFFER_CHUNK_SIZE = 25;
/*     */   protected ArrayList<ProxiedHit> hitBuffer;
/*  61 */   protected int bufferStartOffset = 0;
/*  62 */   protected int bufferEndOffset = 0;
/*  63 */   protected int iterOffset = 0;
/*  64 */   protected boolean atEndOfList = false;
/*     */   
/*     */   protected String server;
/*     */   protected AuthToken authToken;
/*  68 */   protected String targetAcctId = null;
/*  69 */   protected SoapProtocol responseProto = null;
/*     */   
/*     */   private SearchParams searchParams;
/*  72 */   private boolean singleShotRemoteRequest = false;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*  77 */   private long mTimeout = -1L;
/*     */   
/*  79 */   private List<QueryInfo> queryInfo = new ArrayList();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ProxiedQueryResults(SoapProtocol respProto, AuthToken authToken, String targetAccountId, String server, SearchParams params, String queryString, SearchParams.Fetch fetch)
/*     */   {
/*  92 */     super(params.getTypes(), params.getSortBy(), fetch);
/*  93 */     setSearchParams(params, queryString);
/*  94 */     this.authToken = authToken;
/*  95 */     this.server = server;
/*  96 */     this.targetAcctId = targetAccountId;
/*  97 */     this.responseProto = respProto;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ProxiedQueryResults(SoapProtocol respProto, AuthToken authToken, String server, SearchParams params, SearchParams.Fetch fetch)
/*     */   {
/* 108 */     super(params.getTypes(), params.getSortBy(), fetch);
/* 109 */     setSearchParams(params);
/* 110 */     this.authToken = authToken;
/* 111 */     this.server = server;
/* 112 */     this.responseProto = respProto;
/*     */   }
/*     */   
/*     */   private void setSearchParams(SearchParams params) {
/* 116 */     this.searchParams = ((SearchParams)params.clone());
/* 117 */     if ((this.searchParams.getCursor() != null) && (this.searchParams.getLimit() > 0) && (this.searchParams.getLimit() < 500)) {
/* 118 */       SortBy sb = getSortBy();
/*     */       
/* 120 */       if ((sb != null) && ((SortBy.NAME_LOCALIZED_ASC.equals(sb)) || (SortBy.NAME_LOCALIZED_DESC.equals(sb)) || (SortBy.NAME_ASC.equals(sb)) || (SortBy.NAME_DESC.equals(sb))))
/*     */       {
/* 122 */         this.singleShotRemoteRequest = true;
/* 123 */         if (this.searchParams.getLimit() > 0)
/*     */         {
/* 125 */           this.searchParams.setLimit(this.searchParams.getLimit() + 1);
/*     */         }
/*     */       }
/*     */     }
/* 129 */     if (!this.singleShotRemoteRequest) {
/* 130 */       this.searchParams.setCursor(null);
/*     */       
/*     */ 
/* 133 */       this.searchParams.setOffset(0);
/*     */     }
/*     */   }
/*     */   
/*     */   private void setSearchParams(SearchParams params, String queryString) {
/* 138 */     setSearchParams(params);
/* 139 */     this.searchParams.setQueryString(queryString);
/*     */   }
/*     */   
/*     */   public void setTimeout(long timeout) {
/* 143 */     this.mTimeout = timeout;
/*     */   }
/*     */   
/*     */   public long getCursorOffset()
/*     */   {
/* 148 */     return -1L;
/*     */   }
/*     */   
/*     */ 
/*     */   public void close() {}
/*     */   
/*     */   public ZimbraHit skipToHit(int hitNo)
/*     */     throws ServiceException
/*     */   {
/* 157 */     this.iterOffset = hitNo;
/*     */     
/* 159 */     if ((this.iterOffset < this.bufferStartOffset) || (this.iterOffset > this.bufferEndOffset))
/*     */     {
/*     */ 
/*     */ 
/* 163 */       if ((this.iterOffset > this.bufferEndOffset) && (this.atEndOfList)) {
/* 164 */         return null;
/*     */       }
/*     */       
/*     */ 
/* 168 */       this.bufferStartOffset = this.iterOffset;
/* 169 */       this.bufferEndOffset = this.iterOffset;
/* 170 */       this.hitBuffer = null;
/* 171 */       this.atEndOfList = false;
/*     */     }
/*     */     
/*     */ 
/* 175 */     return getNext();
/*     */   }
/*     */   
/*     */   public void resetIterator()
/*     */   {
/* 180 */     this.iterOffset = 0;
/* 181 */     this.atEndOfList = false;
/*     */   }
/*     */   
/*     */   public ZimbraHit getNext() throws ServiceException
/*     */   {
/* 186 */     ZimbraHit retVal = peekNext();
/* 187 */     if (retVal != null) {
/* 188 */       this.iterOffset += 1;
/*     */     }
/* 190 */     return retVal;
/*     */   }
/*     */   
/*     */   public ZimbraHit peekNext() throws ServiceException
/*     */   {
/* 195 */     if ((this.iterOffset >= this.bufferEndOffset) && 
/* 196 */       (!bufferNextHits())) {
/* 197 */       return null;
/*     */     }
/*     */     
/* 200 */     return (ZimbraHit)this.hitBuffer.get(this.iterOffset - this.bufferStartOffset);
/*     */   }
/*     */   
/*     */   String getServer() {
/* 204 */     return this.server;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/*     */     String url;
/*     */     try {
/* 211 */       url = URLUtil.getAdminURL(Provisioning.getInstance().get(Key.ServerBy.name, this.server));
/*     */     } catch (ServiceException ex) {
/* 213 */       url = this.server;
/*     */     }
/* 215 */     return Objects.toStringHelper(this).add("url", url).add("acctId", this.targetAcctId).toString();
/*     */   }
/*     */   
/*     */ 
/*     */   private boolean bufferNextHits()
/*     */     throws ServiceException
/*     */   {
/* 222 */     if ((this.atEndOfList) || (this.searchParams.getHopCount() > 5)) {
/* 223 */       return false;
/*     */     }
/*     */     
/* 226 */     this.bufferStartOffset = this.iterOffset;
/*     */     int chunkSizeToUse;
/*     */     int chunkSizeToUse;
/* 229 */     if (this.singleShotRemoteRequest) {
/* 230 */       chunkSizeToUse = this.searchParams.getLimit();
/*     */     } else {
/* 232 */       chunkSizeToUse = this.searchParams.getLimit() * 2;
/* 233 */       if (chunkSizeToUse < 25) {
/* 234 */         chunkSizeToUse = 25;
/*     */       }
/* 236 */       if (chunkSizeToUse > 500) {
/* 237 */         chunkSizeToUse = 500;
/*     */       }
/*     */     }
/*     */     
/* 241 */     this.bufferEndOffset = (this.bufferStartOffset + chunkSizeToUse);
/* 242 */     this.hitBuffer = new ArrayList(chunkSizeToUse);
/*     */     
/* 244 */     Element searchElt = Element.create(this.responseProto, MailConstants.SEARCH_REQUEST);
/*     */     
/* 246 */     this.searchParams.setOffset(this.bufferStartOffset);
/* 247 */     this.searchParams.setLimit(chunkSizeToUse);
/* 248 */     this.searchParams.encodeParams(searchElt);
/* 249 */     if ((this.singleShotRemoteRequest) && (this.searchParams.getCursor() != null)) {
/* 250 */       Element cursorElt = searchElt.addElement("cursor");
/* 251 */       cursorElt.addAttribute("id", this.searchParams.getCursor().getItemId().getId());
/* 252 */       if (this.searchParams.getCursor().getSortValue() != null) {
/* 253 */         cursorElt.addAttribute("sortVal", this.searchParams.getCursor().getSortValue());
/*     */       }
/* 255 */       if (this.searchParams.getCursor().getEndSortValue() != null) {
/* 256 */         cursorElt.addAttribute("endSortVal", this.searchParams.getCursor().getEndSortValue());
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 261 */     Server targetServer = Provisioning.getInstance().get(Key.ServerBy.name, this.server);
/* 262 */     String baseurl = null;
/*     */     try {
/* 264 */       baseurl = URLUtil.getSoapURL(targetServer, false);
/*     */     }
/*     */     catch (ServiceException e) {}
/* 267 */     if (baseurl == null) {
/* 268 */       baseurl = URLUtil.getAdminURL(targetServer, "/service/admin/soap/", true);
/*     */     }
/* 270 */     ProxyTarget proxy = new ProxyTarget(targetServer, this.authToken, baseurl + MailConstants.SEARCH_REQUEST.getName());
/* 271 */     if (this.mTimeout != -1L) {
/* 272 */       proxy.setTimeouts(this.mTimeout);
/*     */     }
/*     */     
/* 275 */     ZimbraSoapContext zscInbound = this.searchParams.getRequestContext();
/*     */     ZimbraSoapContext zscProxy;
/* 277 */     ZimbraSoapContext zscProxy; if (zscInbound != null) {
/* 278 */       zscProxy = new ZimbraSoapContext(zscInbound, this.targetAcctId);
/*     */     } else {
/* 280 */       zscProxy = new ZimbraSoapContext(this.authToken, this.targetAcctId, this.responseProto, this.responseProto, this.searchParams.getHopCount() + 1);
/*     */     }
/*     */     
/*     */ 
/* 284 */     long start = System.currentTimeMillis();
/* 285 */     Element searchResp = null;
/*     */     try {
/* 287 */       searchResp = DocumentHandler.proxyWithNotification(searchElt, proxy, zscProxy, zscInbound);
/*     */     } catch (SoapFaultException sfe) {
/*     */       long elapsed;
/* 290 */       ZimbraLog.index.warn("Unable to (" + sfe + ") fetch search results from remote server " + proxy);
/* 291 */       this.atEndOfList = true;
/* 292 */       this.bufferEndOffset = this.iterOffset;
/* 293 */       long elapsed; return false;
/*     */     } catch (ServiceException e) { boolean bool1;
/* 295 */       if ("service.PROXY_ERROR".equals(e.getCode())) {
/* 296 */         ZimbraLog.index.warn("Unable to (" + e + ") fetch search results from remote server " + proxy);
/* 297 */         this.atEndOfList = true;
/* 298 */         this.bufferEndOffset = this.iterOffset;
/* 299 */         long elapsed; return false;
/*     */       }
/* 301 */       throw e;
/*     */     } finally {
/* 303 */       long elapsed = System.currentTimeMillis() - start;
/* 304 */       ZimbraLog.index.debug("Remote query took " + elapsed + "ms; URL=" + proxy.toString() + "; QUERY=" + searchElt.toString());
/*     */     }
/*     */     int hitOffset;
/*     */     int hitOffset;
/* 308 */     if (this.singleShotRemoteRequest) {
/* 309 */       hitOffset = (int)searchResp.getAttributeLong("offset", this.bufferStartOffset);
/*     */     } else {
/* 311 */       hitOffset = (int)searchResp.getAttributeLong("offset");
/*     */     }
/* 313 */     boolean hasMore = searchResp.getAttributeBool("more");
/*     */     
/* 315 */     assert (this.bufferStartOffset == hitOffset);
/*     */     
/* 317 */     SortBy sb = getSortBy();
/*     */     
/* 319 */     int bufferIdx = 0;
/* 320 */     int stop = this.bufferEndOffset - this.bufferStartOffset;
/* 321 */     for (Iterator<Element> iter = searchResp.elementIterator(); (iter.hasNext()) && (bufferIdx < stop);) {
/* 322 */       Element el = (Element)iter.next();
/* 323 */       if (el.getName().equalsIgnoreCase("info")) {
/* 324 */         for (Element info : el.listElements()) {
/* 325 */           this.queryInfo.add(new ProxiedQueryInfo(info));
/*     */         }
/*     */         
/* 328 */       } else if ((sb != null) && ((SortBy.NAME_LOCALIZED_ASC.equals(sb)) || (SortBy.NAME_LOCALIZED_DESC.equals(sb)))) {
/* 329 */         this.hitBuffer.add(bufferIdx++, new ProxiedContactHit(this, el, el.getAttribute("fileAsStr")));
/*     */       }
/*     */       else {
/* 332 */         this.hitBuffer.add(bufferIdx++, new ProxiedHit(this, el, el.getAttribute("sf")));
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 338 */     if ((bufferIdx < stop) || (!hasMore))
/*     */     {
/* 340 */       this.bufferEndOffset = (this.bufferStartOffset + bufferIdx);
/*     */       
/* 342 */       if ((hasMore) && 
/* 343 */         (!$assertionsDisabled) && (hasMore)) { throw new AssertionError();
/*     */       }
/* 345 */       this.atEndOfList = true;
/*     */     } else {
/* 347 */       assert (this.bufferEndOffset == this.bufferStartOffset + bufferIdx);
/*     */     }
/* 349 */     if (this.singleShotRemoteRequest) {
/* 350 */       this.atEndOfList = true;
/*     */     }
/*     */     
/* 353 */     assert (this.bufferStartOffset <= this.iterOffset);
/*     */     
/*     */ 
/* 356 */     return this.bufferEndOffset > this.iterOffset;
/*     */   }
/*     */   
/*     */   public List<QueryInfo> getResultInfo()
/*     */   {
/* 361 */     return this.queryInfo;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean isPreSorted()
/*     */   {
/* 370 */     return true;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ProxiedQueryResults.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */